package com.nic.view.util.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * 网络改变监控广播
 * <p>
 * 监听网络的改变状态,只有在用户操作网络连接开关(wifi,mobile)的时候接受广播,
 * 然后对相应的界面进行相应的操作，并将 状态 保存在我们的APP里面
 * <p>
 * <p>
 * Created by xujun
 */
public class NetworkConnectChangedReceiver extends BroadcastReceiver {

    public final static String NET_2G = "2G";
    public final static String NET_3G = "3G";
    public final static String NET_4G = "4G";
    public final static String NET_WIFI = "WIFI";
    public final static String NET_UNKNOW = "UNKNOW";
    private boolean enableWifi;//wifi打开状态
    private boolean wifi;//是否连接wifi
    private boolean mobile;//是否连接移动数据流量
    private boolean connected;//是否连接网络
    private String mobileType = NET_UNKNOW;//网络类型

    public String getMobileType() {
        return mobileType;
    }

    public boolean isConnected() {
        return connected;
    }

    public void setConnected(boolean connected) {
        this.connected = connected;
    }

    public boolean isEnableWifi() {
        return enableWifi;
    }

    public void setEnableWifi(boolean enableWifi) {
        this.enableWifi = enableWifi;
    }

    public boolean isWifi() {
        return wifi;
    }

    public void setWifi(boolean wifi) {
        this.wifi = wifi;
    }

    public boolean isMobile() {
        return mobile;
    }

    public void setMobile(boolean mobile) {
        this.mobile = mobile;
    }

    private static final String TAG = "TAG";
    public static final String TAG1 = "TAG1";

    @Override
    public void onReceive(Context context, Intent intent) {
        // 这个监听wifi的打开与关闭，与wifi的连接无关
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            Log.e(TAG1, "wifiState" + wifiState);
            switch (wifiState) {
                case WifiManager.WIFI_STATE_DISABLED://已关闭
                    setEnableWifi(false);
                    break;
                case WifiManager.WIFI_STATE_DISABLING://关闭中
                    break;
                case WifiManager.WIFI_STATE_ENABLING://连接中
                    break;
                case WifiManager.WIFI_STATE_ENABLED://已连接
                    setEnableWifi(true);
                    break;
                case WifiManager.WIFI_STATE_UNKNOWN://
                    setEnableWifi(false);
                    break;
                default:
                    setEnableWifi(false);
                    break;


            }
        }
        // 这个监听wifi的连接状态即是否连上了一个有效无线路由，当上边广播的状态是WifiManager
        // .WIFI_STATE_DISABLING，和WIFI_STATE_DISABLED的时候，根本不会接到这个广播。
        // 在上边广播接到广播是WifiManager.WIFI_STATE_ENABLED状态的同时也会接到这个广播，
        // 当然刚打开wifi肯定还没有连接到有效的无线
        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            Parcelable parcelableExtra = intent
                    .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (null != parcelableExtra) {
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                NetworkInfo.State state = networkInfo.getState();
                boolean isConnected = state == NetworkInfo.State.CONNECTED;// 当然，这边可以更精确的确定状态
                Log.e(TAG1, "wifi isConnected" + isConnected);
                if (enableWifi && isConnected) {
                    mobileType = NET_WIFI;
                    setWifi(true);
                } else {
                    setWifi(false);
                }
            }
        }
        // 这个监听网络连接的设置，包括wifi和移动数据的打开和关闭。.
        // 最好用的还是这个监听。wifi如果打开，关闭，以及连接上可用的连接都会接到监听。见log
        // 这个广播的最大弊端是比上边两个广播的反应要慢，如果只是要监听wifi，我觉得还是用上边两个配合比较合适
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            ConnectivityManager manager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            Log.i(TAG1, "CONNECTIVITY_ACTION");

            NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
            if (activeNetwork != null) { // connected to the internet
                if (activeNetwork.isConnected()) {
                    networkSate(activeNetwork.getType(), activeNetwork);
                } else {
                    Log.e(TAG, "当前没有网络连接，请确保你已经打开网络 ");
                }
//                Log.e(TAG1, "info.getTypeName()" + activeNetwork.getTypeName());
//                Log.e(TAG1, "getSubtypeName()" + activeNetwork.getSubtypeName());
//                Log.e(TAG1, "getState()" + activeNetwork.getState());
//                Log.e(TAG1, "getDetailedState()"
//                        + activeNetwork.getDetailedState().name());
//                Log.e(TAG1, "getDetailedState()" + activeNetwork.getExtraInfo());
//                Log.e(TAG1, "getType()" + activeNetwork.getType());
            } else {   // not connected to the internet
                Log.e(TAG, "当前没有网络连接，请确保你已经打开网络 ");
                setMobile(false);
            }
        }
        if (isWifi() || isMobile()) {
            setConnected(true);
        } else {
            setConnected(false);
        }

    }


    /**
     * 检测移动网络
     *
     * @param type1
     * @param activeNetwork
     */
    private void networkSate(int type1, NetworkInfo activeNetwork) {
        String type;
        if (type1 == ConnectivityManager.TYPE_MOBILE) {
            // connected to the mobile provider's data plan
            Log.e(TAG, "当前移动网络连接可用 ");
            type = "移动网络";
            setMobile(false);
            switch (activeNetwork.getState()) {
                case CONNECTED:
                    Log.i(TAG1, type + "连接成功");
                    setMobile(true);
                    switch (activeNetwork.getSubtype()) {
                        case TelephonyManager.NETWORK_TYPE_GSM:
                        case TelephonyManager.NETWORK_TYPE_GPRS:
                        case TelephonyManager.NETWORK_TYPE_CDMA:
                        case TelephonyManager.NETWORK_TYPE_EDGE:
                        case TelephonyManager.NETWORK_TYPE_1xRTT:
                        case TelephonyManager.NETWORK_TYPE_IDEN:
                            mobileType = NET_2G;
                            break;

                        case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
                        case TelephonyManager.NETWORK_TYPE_EVDO_A:
                        case TelephonyManager.NETWORK_TYPE_UMTS:
                        case TelephonyManager.NETWORK_TYPE_EVDO_0:
                        case TelephonyManager.NETWORK_TYPE_HSDPA:
                        case TelephonyManager.NETWORK_TYPE_HSUPA:
                        case TelephonyManager.NETWORK_TYPE_HSPA:
                        case TelephonyManager.NETWORK_TYPE_EVDO_B:
                        case TelephonyManager.NETWORK_TYPE_EHRPD:
                        case TelephonyManager.NETWORK_TYPE_HSPAP:
                            mobileType = NET_3G;
                            break;
                        case TelephonyManager.NETWORK_TYPE_IWLAN:
                        case TelephonyManager.NETWORK_TYPE_LTE:
                            mobileType = NET_4G;
                            break;
                        default:
                            String subtypeName = activeNetwork.getSubtypeName();
                            if (subtypeName.equalsIgnoreCase("TD-SCDMA")
                                    || subtypeName.equalsIgnoreCase("WCDMA")
                                    || subtypeName.equalsIgnoreCase("CDMA2000")) {
                                mobileType = NET_3G;
                            } else {
                                mobileType = NET_UNKNOW;
                            }
                            break;
                    }

                    break;
                case DISCONNECTED:

                    Log.i(TAG1, type + "连接失败");
                    break;
                case UNKNOWN:
                    Log.i(TAG1, type + "不知道");
                    break;
                default:
                    break;
            }
        }

    }
    //    private void registerNetLis() {
//        IntentFilter filter = new IntentFilter();
//        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
//        filter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
//        filter.addAction("android.net.wifi.STATE_CHANGE");
//        registerReceiver(mNetworkChangeListener, filter);
//    }
//
//    NetworkConnectChangedReceiver mNetworkChangeListener = new NetworkConnectChangedReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            super.onReceive(context, intent);
//            if (isConnected()) {
//                isOnline = true;
//            } else {
//                isOnline = false;
//            }
//            refreshNet(isOnline);
//        }
//
//    };
}
